<?php
include_once('common/header.php');
if($AppUI->id <= 0 || (isset($_SESSION['slidershow']) && (
							($_SESSION['slidershow']&1<<1) == 0 &&
							($_SESSION['slidershow']&1<<2) == 0
						))) {
	$this->_redirect(HOST_BACKEND);
}
$intType		= $objRequest->getParam('ptype', EDUCATION_TYPE);
$intForm		= $objRequest->getParam('pform', 0);
$intAlbumId		= $objRequest->getParam('palbumid', 0);
$strAlbumName 	= $objRequest->getParam('pal_name', '');
$strName		= $objRequest->getParam('pname', '');
$oFile			= $_FILES['pimage'];
$strAlbumName	= Ecore_Function::stripTags($strAlbumName,-2);
$strName		= Ecore_Function::stripTags($strName,-2);
$arrErr			= null;
if($intForm == 0) {// CREATE ALBUM AND UPLOAD			
	if(!isset($strAlbumName) && $strAlbumName == '') array_push($arrErr, $arrLocale['album_name_required']);
	elseif($strAlbumName != '' && strlen($strAlbumName) < VL_NAME_MINLENGTH) array_push($arrErr, $arrLocale['album_name_minlength']);
	if(isset($strName) && $strName != '' && strlen($strName) < VL_NAME_MINLENGTH) array_push($arrErr, $arrLocale['image_name_minlength']);
	$strImage	= '';
	if(!isset($arrErr) && isset($oFile)) {
		$arrImage = null;
		if($intType == EDUCATION_TYPE) {
			$arrImage	= Ecore_Upload_Education::upload($oFile);		
		}
		if(isset($arrImage['error']) && $arrImage['error'] == 0) {
			$strImage			= $arrImage['path'];
		}else {
			array_push($arrErr, $arrLocale['upload']);
		}
	}
	if(!isset($arrErr)) {
		if(isset($_SESSION['SLIDESHOW_POST'])) unset($_SESSION['SLIDESHOW_POST']);
		if(isset($_SESSION['SLIDESHOW_ERROR'])) unset($_SESSION['SLIDESHOW_ERROR']);		
		$objSlideshow 	= new Slideshow();
		$intError		= $objSlideshow->addAlbum($intType, 
												  $AppUI->id, 
												  $strAlbumName, 
												  $strImage, 
												  $strName
												);
		if($intError == 0) Ecore_Function::setMsg($arrMessage['upload_add']);
		else array_push($arrErr, $arrError['noperm_upload_add']);
	}
	if(isset($arrErr)) {
		$_SESSION['SLIDESHOW_POST']		= $objRequest->getParams();
		$_SESSION['SLIDESHOW_ERROR']	= $arrErr;
		$this->_redirect(HOST_BACKEND . 'slideshow/upload?type=' . $intType);
	}
}elseif($intForm == 1) {// UPLOAD IMAGE TO ALBUM
	if(isset($strName) && $strName != '' && strlen($strName) < VL_NAME_MINLENGTH) array_push($arrErr, $arrLocale['image_name_minlength']);
	$strImage		= '';	
	if(!isset($arrErr) && isset($oFile)) {
		$arrImage = null;
		if($intType == EDUCATION_TYPE) {
			$arrImage	= Ecore_Upload_Education::upload($oFile);		
		}
		if(isset($arrImage['error']) && $arrImage['error'] == 0) {
			$strImage			= $arrImage['path'];
		}else {
			array_push($arrErr, $arrLocale['upload']);
		}
	}	
	if(!isset($arrErr)) {
		if(isset($_SESSION['SLIDESHOW_POST'])) unset($_SESSION['SLIDESHOW_POST']);
		if(isset($_SESSION['SLIDESHOW_ERROR'])) unset($_SESSION['SLIDESHOW_ERROR']);		
		$objSlideshow 	= new Slideshow();		
		$intError		= $objSlideshow->addImage($intType,
												  $AppUI->id, 
												  $intAlbumId, 
												  $strImage, 
												  $strName);
		if($intError == 0) Ecore_Function::setMsg($arrMessage['upload_img']);
		else array_push($arrErr, $arrError['noperm_upload_img']);
	}
	if(isset($arrErr)) {
		$_SESSION['SLIDESHOW_POST']		= $objRequest->getParams();
		$_SESSION['SLIDESHOW_ERROR']	= $arrErr;	
	}
	$this->_redirect(HOST_BACKEND . 'slideshow/upload?form='. $intForm . '&albumid=' . $intAlbumId . '&type=' . $intType);
}elseif($intForm == 2) {//UPDATE ALBUM INFO
	if(!isset($arrErr)) {
		if(isset($_SESSION['SLIDESHOW_POST'])) unset($_SESSION['SLIDESHOW_POST']);
		if(isset($_SESSION['SLIDESHOW_ERROR'])) unset($_SESSION['SLIDESHOW_ERROR']);		
		$objSlideshow 	= new Slideshow();
		$intError		= $objSlideshow->updateAlbum($AppUI->id, 
													 $intAlbumId, 
													 $strAlbumName
													);
		if($intError == 0) Ecore_Function::setMsg($arrMessage['upload_edit']);
		else array_push($arrErr, $arrError['noperm_upload_edit']);
	}
	if(isset($arrErr)) {
		$_SESSION['SLIDESHOW_POST']		= $objRequest->getParams();
		$_SESSION['SLIDESHOW_ERROR']	= $arrErr;
		$this->_redirect(HOST_BACKEND . 'slideshow/upload?form='. $intForm . '&albumid=' . $intAlbumId . '&type=' . $intType);
	}
}
$this->_redirect(isset($_SESSION[SESSION_BACKURL]) ? $_SESSION[SESSION_BACKURL] : (HOST_BACKEND . 'slideshow?type=' . $intType));